//
//  GQHContractDetailView.m
//  Xanthium
//
//  Created by GuanQinghao on 09/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHContractDetailView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"

@interface GQHContractDetailView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *sectionTitleArray;
}

@end

@implementation GQHContractDetailView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    sectionTitleArray = @[@"协议信息",@"托运信息",@"承运信息",@"郑重声明"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 960.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHContractCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHContractCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.qh_totalFeeLabel.text = [NSString stringWithFormat:@"总费用：%.2f元",_qh_contractModel.qh_contractAmount];
    cell.qh_totalFeeLabel1.text = [NSString stringWithFormat:@"总费用：%.2f元",_qh_contractModel.qh_contractAmount];
    cell.qh_contractTimeLabel.text = [NSString stringWithFormat:@"协议日期：%@",_qh_contractModel.qh_contractTime];
    cell.qh_routeLabel.text = [NSString stringWithFormat:@"%@-%@",_qh_contractModel.qh_departure,_qh_contractModel.qh_destination];
    
    cell.qh_contentLabel.text = [NSString stringWithFormat:@"货物信息：%@/%.2f/%@/%@",_qh_contractModel.qh_freightName,_qh_contractModel.qh_freightWeight,_qh_contractModel.qh_modelWanted,_qh_contractModel.qh_lengthWanted];
    cell.qh_packTimeLabel.text = [NSString stringWithFormat:@"装车日期：%@",_qh_contractModel.qh_loadingTime];
    /// 头像
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_contractModel.qh_ownerAvatar];
    NSURL *URL = [NSURL URLWithString:URLString];
    [cell.qh_shipperAvatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    
    cell.qh_shipperNameLabel.text = _qh_contractModel.qh_ownerName;
    cell.driverName.text = _qh_contractModel.qh_consignee;
    cell.consigneeMobile.text = _qh_contractModel.qh_consigneeMobile;
    cell.ownerName.text  =  _qh_contractModel.qh_ownerName;
    
    GQHContractExpenseModel *espenseModel = _qh_contractModel.qh_contractExpenseModel;
    NSArray<GQHContractPaymentModel *> *prepaymentModel = espenseModel.qh_contractPrepayment;
    NSArray<GQHContractPaymentModel *> *finalPaymentModel = espenseModel.qh_contractFinalPayment;
    NSArray<GQHContractPaymentModel *> *otherExpenseModel = espenseModel.qh_contractOtherExpense;
    
    CGFloat prepaidAmount = 0.0f;
    for (GQHContractPaymentModel *model in prepaymentModel) {
        
        if ([model.qh_paymentType isEqualToString:@"01"]) {
            
            cell.qh_preGasLabel.text = [NSString stringWithFormat:@"油卡：%@元",model.qh_paymentAmount];
            prepaidAmount += [model.qh_paymentAmount floatValue];
        } else if ([model.qh_paymentType isEqualToString:@"02"]) {
            
            cell.qh_preETCLabel.text = [NSString stringWithFormat:@"ETC：%@元",model.qh_paymentAmount];
            prepaidAmount += [model.qh_paymentAmount floatValue];
        } else if ([model.qh_paymentType isEqualToString:@"03"]) {
            
            cell.qh_preCashLabel.text = [NSString stringWithFormat:@"现金：%@元",model.qh_paymentAmount];
            prepaidAmount += [model.qh_paymentAmount floatValue];
        } else {
            
        }
    }
    cell.qh_prepaidLabel.text = [NSString stringWithFormat:@"预付款合计：%.2f元",prepaidAmount];
    cell.qh_prepaidLabel.text = [NSString stringWithFormat:@"预付款合计：%.2f元",prepaidAmount];
    cell.qh_prepaidLabel.text = [NSString stringWithFormat:@"预付款合计：%.2f元",prepaidAmount];
    
    CGFloat postpaidAmount = 0.0f;
    for (GQHContractPaymentModel *model in finalPaymentModel) {
        
        if ([model.qh_paymentType isEqualToString:@"01"]) {
            
            cell.qh_postGasLabel.text = [NSString stringWithFormat:@"油卡：%@元",model.qh_paymentAmount];
            postpaidAmount += [model.qh_paymentAmount floatValue];
        } else if ([model.qh_paymentType isEqualToString:@"02"]) {
            
            cell.qh_postETCLabel.text = [NSString stringWithFormat:@"ETC：%@元",model.qh_paymentAmount];
            postpaidAmount += [model.qh_paymentAmount floatValue];
        } else if ([model.qh_paymentType isEqualToString:@"03"]) {
            
            cell.qh_postCashLabel.text = [NSString stringWithFormat:@"现金：%@元",model.qh_paymentAmount];
            postpaidAmount += [model.qh_paymentAmount floatValue];
        } else {
            
        }
    }
    cell.qh_postpaidLabel.text = [NSString stringWithFormat:@"尾款合计：%.2f元",postpaidAmount];
    
    
    cell.qh_weightLabel.text = [NSString stringWithFormat:@"货物重量：%.2f吨",_qh_contractModel.qh_freightWeight];
    
    cell.qh_volumeLabel.text = [NSString stringWithFormat:@"货物体积：%.2f方",_qh_contractModel.qh_freightVolume];
    
    cell.qh_quantityLabel.text = [NSString stringWithFormat:@"货物数量：%.2f件",_qh_contractModel.qh_freightNumber];
    
//    GQHContractPaymentModel *otherModel = otherExpenseModel[0];
//    cell.qh_otherFeeLabel.text = [NSString stringWithFormat:@"其他费用：%@元",otherModel.qh_paymentAmount];
//    cell.qh_noteLabel.text = _qh_contractModel.qh_contractOtherExpenseNote;
//
    
    NSString *URLString1 = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_contractModel.qh_driverAvatar];
    NSURL *URL1 = [NSURL URLWithString:URLString1];
    [cell.qh_driverAvatarImageView sd_setImageWithURL:URL1 placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    cell.qh_driverNameLabel.text = _qh_contractModel.qh_driverName;
    cell.qh_carNumberLabel.text = [NSString stringWithFormat:@"车牌号：%@",_qh_contractModel.qh_licensePlateNumber];
    cell.qh_carInfoLabel.text = [NSString stringWithFormat:@"车辆信息：%@/%@/%@",_qh_contractModel.qh_model,_qh_contractModel.qh_normalLength,_qh_contractModel.qh_normalLoading];
    
    [cell.qh_contentWebView loadHTMLString:_qh_contractModel.qh_contractContent baseURL:nil];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

@end
